package cn.tedu.lucky.draw.server.job.config;

import cn.tedu.lucky.draw.server.job.listener.CsvToDBListener;
import cn.tedu.lucky.draw.server.job.processor.ActivityItemProcessor;
import cn.tedu.lucky.draw.server.protocol.po.Activity;
import cn.tedu.lucky.draw.server.repository.ActivityRepository;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.data.RepositoryItemWriter;
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.PathResource;

import java.io.File;

@Configuration
public class CsvToDBJobConfig {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    private ActivityRepository activityRepository;


    @Bean
    public ItemReader<Activity> itemReader() {
        return new FlatFileItemReaderBuilder<Activity>()
                .name("csv-to-db-reader") //设置ItemReader的名称
                .saveState(false)//设置是否保存状态，如果作业失败重启，是否需要从上次停止的地方继续
                .resource(new PathResource(new File("doc/activity.csv").getAbsolutePath()))
                .delimited() //设置文件分隔符
                .names("name","description","img","status","sort")
                .targetType(Activity.class) //设置读取的目标类型
                .linesToSkip(1) //设置在开始读取数据之前，要跳过的行数，这里设置为1，因为第一行是标题
                .build();
    }

    @Bean
    public ActivityItemProcessor activityItemProcessor() {
        return new ActivityItemProcessor();
    }

    @Bean
    public ItemWriter<Activity> itemWriter(){
        RepositoryItemWriter<Activity> activityRepositoryItemWriter = new RepositoryItemWriter<>();
        activityRepositoryItemWriter.setRepository(activityRepository);
        activityRepositoryItemWriter.setMethodName("save");
        return activityRepositoryItemWriter;
    }

    @Bean
    public Step csvToDbStep() {
        return stepBuilderFactory.get("csv-to-db-step")
                .<Activity, Activity>chunk(10000)
                .reader(itemReader())
                .processor(activityItemProcessor())
                .writer(itemWriter())
                .build();
    }

    @Bean
    public CsvToDBListener csvToDbListener() {
        return new CsvToDBListener();
    }

    @Bean
    public Job csvToDbJob() {
        return jobBuilderFactory.get("csv-to-db-job")
                .start(csvToDbStep())
                /**
                 * 设置作业的ID，如果作业失败重启，需要用到该ID
                 * RunIdIncrementer会在每次作业执行时，生成一个新的ID
                 */
                .incrementer(new RunIdIncrementer())
                .listener(csvToDbListener())
                .build();
    }

}
